﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
using MetroLightKit.Helpers;

namespace MetroLightKit.Controls
{
    public class MetroButtonContent : ContentControl
    {
        public static readonly DependencyProperty ImageMarginProperty = DependencyProperty.Register("ImageMargin",
                                                                                                    typeof (Thickness),
                                                                                                    typeof (
                                                                                                        MetroButtonContent
                                                                                                        ), null);

        public static readonly DependencyProperty ImageScaleYProperty = DependencyProperty.Register("ImageScaleY",
                                                                                                    typeof (double),
                                                                                                    typeof (
                                                                                                        MetroButtonContent
                                                                                                        ), null);

        public static readonly DependencyProperty ImageScaleXProperty = DependencyProperty.Register("ImageScaleX",
                                                                                                    typeof (double),
                                                                                                    typeof (
                                                                                                        MetroButtonContent
                                                                                                        ), null);

        public Thickness ImageMargin
        {
            get { return (Thickness) base.GetValue(ImageMarginProperty); }
            set { base.SetValue(ImageMarginProperty, value); }
        }

        public double ImageScaleX
        {
            get { return (double) base.GetValue(ImageScaleXProperty); }
            set { base.SetValue(ImageScaleXProperty, value); }
        }

        public double ImageScaleY
        {
            get { return (double) base.GetValue(ImageScaleYProperty); }
            set { base.SetValue(ImageScaleYProperty, value); }
        }


        public override void OnApplyTemplate()
        {
            var panel = this.FindVisualChild<Panel>();
            if (panel != null)
            {
                    
                var bind = new Binding("ImageMargin")
                                {RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent)};
                panel.SetBinding(MarginProperty, bind);

                if (!ImageScaleX.Equals(0.0) && !ImageScaleY.Equals(0.0))
                {
                    panel.RenderTransform = new CompositeTransform();
                    var bindY = new Binding("ImageScaleY") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) };
                    var bindX = new Binding("ImageScaleX") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) };
                    if (!ImageScaleX.Equals(0.0) || !ImageScaleY.Equals(0.0))
                            panel.RenderTransformOrigin = new Point(0.5, 0.5);
                    BindingOperations.SetBinding(panel.RenderTransform,
                                                    CompositeTransform.ScaleXProperty, bindX);
                    BindingOperations.SetBinding(panel.RenderTransform,
                                                    CompositeTransform.ScaleYProperty, bindY);
                        
                }
            }
            foreach (Shape shape in this.FindChilds<Shape>())
            {
                var bind = new Binding("Foreground")
                                {RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent)};
                shape.SetBinding(Shape.FillProperty, bind);
            }
            base.OnApplyTemplate();
        }
    }
}